{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-2-f0b0b92a2370>, line 68)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-2-f0b0b92a2370>\"\u001b[1;36m, line \u001b[1;32m68\u001b[0m\n\u001b[1;33m    if__name__=='__main__':\u001b[0m\n\u001b[1;37m                           ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "import straing,re\n",
    "def load_dict(filename):\n",
    "    f=open(filename,'r',encoding='utf8').read()\n",
    "    maxLen=1\n",
    "    dict=f.split(\"\\n\")\n",
    "    for i in dict:\n",
    "        if len(i)>maxLen:\n",
    "            maxLen=len(i)\n",
    "    return dict,maxLen;\n",
    "def FMM(dict,maxLen,Lext):\n",
    "    cut_word=[]\n",
    "    start=0\n",
    "    textLen=len(text)\n",
    "    while start !=textLen:\n",
    "        index =start+maxLen\n",
    "        if index>len(text):\n",
    "            index=len(text)\n",
    "        for i in range(maxLen):\n",
    "            if(text[start:index]in dict)or(len(text[start:index])==1):\n",
    "                cut_word.append(text[start:index])\n",
    "                start =index\n",
    "                break\n",
    "            index-=1\n",
    "    return cut_word\n",
    "def RMM(dict,maxLen,text):\n",
    "    cut_word=[]\n",
    "    textLen=len(text)\n",
    "    while textLen>0:\n",
    "        j=0\n",
    "        for size in range(maxLen,0,-1):\n",
    "            if textLen<size:\n",
    "                continue\n",
    "            cutword=text[textLen-size:textLen]\n",
    "            if cutword in dict:\n",
    "                cut_word.append(cutword)\n",
    "                textLen-=size\n",
    "                j+=1\n",
    "                break\n",
    "        if j==0:\n",
    "            textLen-=1\n",
    "    cut_word=list(reversed(cut_word))\n",
    "    return cut_word\n",
    "def BMM(dict,maxLen,text):\n",
    "    fmm=FMM(dict,maxLen,text)\n",
    "    rmm=RMM(dict,maxLen,text)\n",
    "    if len(fmm)!=len(rmm):\n",
    "        if len(fmm)<len(rmm):\n",
    "            return fmm\n",
    "        else:\n",
    "            return rmm\n",
    "    else:\n",
    "        if fmm==rmm:\n",
    "            return fmm\n",
    "        else:\n",
    "            fmm_1num=len([i for i in fmm if len(i)==1])\n",
    "            rmm_1num=len([i for i in rmm if len(i)==1])\n",
    "            return fmm_1num if fmm_1num<rmm_1num else rmm_1num\n",
    "def main():\n",
    "    dict,maxLen=load_dict('data/dict1.txt')\n",
    "    print(\"词典中的最大词长:\",maxLen)\n",
    "    text=\"南京市长江大桥非常宏伟\"\n",
    "    fmm_cut=FMM(dict,maxLen,text)\n",
    "    print(\"正向最大匹配法FMM,分词结果:\",fmm_cut)\n",
    "    rmm_cut=RMM(dict,maxLen,text)\n",
    "    print(\"逆向最大匹配法RMM,分词结果:\",rmm_cut)\n",
    "    bmm_cut=BMM(dict,maxLen,text)\n",
    "    print(\"双向最大匹配法BMM,分词结果:\",bmm_cut)\n",
    "if__name__=='__main__':\n",
    "    main()\n",
    "    \n",
    "    \n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
